package com.example.sample.activity;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.R.drawable;
import com.example.sample.R.id;
import com.example.sample.R.layout;

public class TextViewMeasureActivity extends BaseActivity {
	
	private TextView tv;
	private TextView show;
	private LinearLayout root;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_textview_measure);
		tv = (TextView)findViewById(R.id.tv);
		show = (TextView)findViewById(R.id.show);
		root = (LinearLayout)findViewById(R.id.root);
		
		EditText mEditText = new EditText(this);
		mEditText.setText("卧槽");
		mEditText.setSingleLine();
		mEditText.setBackgroundResource(R.drawable.bar_bottom);
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, 200);
		mEditText.setLayoutParams(lp);
		mEditText.setSingleLine();
		mEditText.setLines(1);
		mEditText.setMaxLines(1);
		mEditText.setMinLines(1);
		mEditText.setTextColor(Color.parseColor("#ffffff"));
		root.addView(mEditText);
	}
	
	public int getTvWidth(){
		Paint paint = tv.getPaint();
		String txt = (String)tv.getText();
		float w = paint.measureText(txt);
		return (int) Math.ceil(w);
	}
	
	public int getTvWidth2(){
		Paint paint = new Paint();
		String txt = (String)tv.getText();
		paint.setTextSize(45);
		float w = paint.measureText(txt);
		return (int) Math.ceil(w);
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		
		String out = "getWidth = "+ tv.getWidth() + "\n" +
				"getTvWidth = "+ getTvWidth() + "\n" +
				"getTvWidth2 = "+ getTvWidth2();
		
		show.setText(out);
	}

}
